package mapeditor;

import java.awt.Image;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class EditorFrame extends javax.swing.JFrame {

    static java.lang.String sTemp;
    static char[][] testarray;
    static int i,  j;

    public EditorFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new PaintPanel();
        jPanel2 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jBild = new javax.swing.JButton();
        jSave = new javax.swing.JButton();
        jChange = new javax.swing.JButton();
        jCancel = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jFile = new javax.swing.JMenu();
        jMenuItemChange = new javax.swing.JMenuItem();
        jMenuItemPhote = new javax.swing.JMenuItem();
        jMenuItemSave = new javax.swing.JMenuItem();
        jMenuItemClose = new javax.swing.JMenuItem();

        FormListener formListener = new FormListener();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("robofight");

        jPanel1.setMinimumSize(new java.awt.Dimension(800, 550));
        jPanel1.setPreferredSize(new java.awt.Dimension(800, 550));
        jPanel1.setLayout(null);
        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.LINE_AXIS));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Blocked ", "Unblock", "Starting point " }));
        jComboBox1.setMaximumSize(new java.awt.Dimension(120, 20));
        jComboBox1.setMinimumSize(new java.awt.Dimension(50, 20));
        jComboBox1.setPreferredSize(new java.awt.Dimension(50, 20));
        jComboBox1.addActionListener(formListener);
        jPanel2.add(jComboBox1);

        jBild.setText("Bild Laden");
        jBild.addActionListener(formListener);
        jPanel2.add(jBild);

        jSave.setText("Speichern");
        jSave.addActionListener(formListener);
        jPanel2.add(jSave);

        jChange.setText("Map Laden");
        jChange.addActionListener(formListener);
        jPanel2.add(jChange);

        jCancel.setText("Schließen");
        jCancel.addActionListener(formListener);
        jPanel2.add(jCancel);

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

        jFile.setText("File");

        jMenuItemChange.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemChange.setText("Öffnen");
        jMenuItemChange.addActionListener(formListener);
        jFile.add(jMenuItemChange);

        jMenuItemPhote.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_B, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemPhote.setText("Bild laden");
        jMenuItemPhote.addActionListener(formListener);
        jFile.add(jMenuItemPhote);

        jMenuItemSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemSave.setText("Speichern");
        jMenuItemSave.addActionListener(formListener);
        jFile.add(jMenuItemSave);

        jMenuItemClose.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItemClose.setText("Schließen");
        jMenuItemClose.addActionListener(formListener);
        jFile.add(jMenuItemClose);

        jMenuBar1.add(jFile);

        setJMenuBar(jMenuBar1);

        pack();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == jComboBox1) {
                EditorFrame.this.jComboBox1ActionPerformed(evt);
            }
            else if (evt.getSource() == jBild) {
                EditorFrame.this.jBildActionPerformed(evt);
            }
            else if (evt.getSource() == jSave) {
                EditorFrame.this.jSaveActionPerformed(evt);
            }
            else if (evt.getSource() == jChange) {
                EditorFrame.this.jChangeActionPerformed(evt);
            }
            else if (evt.getSource() == jCancel) {
                EditorFrame.this.jCancelActionPerformed(evt);
            }
            else if (evt.getSource() == jMenuItemChange) {
                EditorFrame.this.jMenuItemChangeActionPerformed(evt);
            }
            else if (evt.getSource() == jMenuItemPhote) {
                EditorFrame.this.jMenuItemPhoteActionPerformed(evt);
            }
            else if (evt.getSource() == jMenuItemSave) {
                EditorFrame.this.jMenuItemSaveActionPerformed(evt);
            }
            else if (evt.getSource() == jMenuItemClose) {
                EditorFrame.this.jMenuItemCloseActionPerformed(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        PaintPanel paintPanel = (PaintPanel) jPanel1;
        String name = (String) (jComboBox1.getSelectedItem());
        paintPanel.setColor(Field.name2Color.get(name));
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jBildActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBildActionPerformed
        JFileChooser jfc = new JFileChooser();
        jfc.setCurrentDirectory(new File("."));

        if (jfc.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
            return;
        }

        try {
            PaintPanel paintPanel = (PaintPanel) jPanel1;
            Image image = ImageIO.read(jfc.getSelectedFile());
            paintPanel.setImage(image);
        } catch (IOException ex) {
            Logger.getLogger(EditorFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_jBildActionPerformed

    private void jSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSaveActionPerformed
        JFileChooser jfc = new JFileChooser();
        jfc.setCurrentDirectory(new File("."));
        if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
            PaintPanel paintPanel = (PaintPanel) jPanel1;
            paintPanel.save(jfc.getSelectedFile());
        }
}//GEN-LAST:event_jSaveActionPerformed

    private void jMenuItemChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemChangeActionPerformed
        JFileChooser jfc = new JFileChooser();
        jfc.setCurrentDirectory(new File("."));
        if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
            PaintPanel paintPanel = (PaintPanel) jPanel1;
            paintPanel.save(jfc.getSelectedFile());
        }
}//GEN-LAST:event_jMenuItemChangeActionPerformed

    private void jMenuItemPhoteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemPhoteActionPerformed
        JFileChooser jfc = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Bild", "jpg");
        jfc.setAcceptAllFileFilterUsed(false);
        jfc.setMultiSelectionEnabled(false);
        jfc.addChoosableFileFilter(filter);
        jfc.setDialogTitle("Wählen Sie Ihre Hintergrundbild aus:");
        //jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);


        if (jfc.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
            return;
        }

        try {
            PaintPanel paintPanel = (PaintPanel) jPanel1;
            Image image = ImageIO.read(jfc.getSelectedFile());
            paintPanel.setImage(image);
        } catch (IOException ex) {
            Logger.getLogger(EditorFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_jMenuItemPhoteActionPerformed

    private void jMenuItemSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveActionPerformed
        JFileChooser fileChooser1 = new JFileChooser();
        fileChooser1.setCurrentDirectory(new File("."));
        if (fileChooser1.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
            PaintPanel paintPanel = (PaintPanel) jPanel1;
            paintPanel.save(fileChooser1.getSelectedFile());
        }

}//GEN-LAST:event_jMenuItemSaveActionPerformed

    private void jMenuItemCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemCloseActionPerformed
        System.exit(0);
}//GEN-LAST:event_jMenuItemCloseActionPerformed

    private void jChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jChangeActionPerformed
        JFileChooser fileChooser1 = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Maps", "txt");
        fileChooser1.setAcceptAllFileFilterUsed(false);
        fileChooser1.setMultiSelectionEnabled(false);
        fileChooser1.addChoosableFileFilter(filter);
        if (fileChooser1.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
            return;
        }
        try {
            testarray = new char[55][80];
            BufferedReader oReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileChooser1.getSelectedFile())));
            i = 0;
            try {
                while ((sTemp = oReader.readLine()) != null) {
                    char[] tmp = sTemp.toCharArray();
                    for (int r = 0; r < tmp.length; r++) {
                        testarray[i][r] = tmp[r];
                    }
                    i++;
                }
                for (int zeile = 0; zeile < 55; zeile++) {
                    for (int spalte = 0; spalte < 80; spalte++) {
                        System.out.print(testarray[zeile][spalte]);
                    }
                    System.out.println("");

                }
                PaintPanel pp = (PaintPanel) jPanel1;
                pp.setLoadedPanel(testarray);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
//                Logger.getLogger(EditorFrame.class.getName()).log(Level.SEVERE, null, e);
            e.printStackTrace();
        }       

}//GEN-LAST:event_jChangeActionPerformed

    private void jCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBild;
    private javax.swing.JButton jCancel;
    private javax.swing.JButton jChange;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JMenu jFile;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItemChange;
    private javax.swing.JMenuItem jMenuItemClose;
    private javax.swing.JMenuItem jMenuItemPhote;
    private javax.swing.JMenuItem jMenuItemSave;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton jSave;
    // End of variables declaration//GEN-END:variables

    private static class FileFilterImpl implements FileFilter {

        public FileFilterImpl() {
        }

        public boolean accept(File f) {
            return f.getName().toLowerCase().endsWith(".txt") || f.isDirectory();
        }
    }
}
